package com.wc.算法提高课.E第五章_数学知识.筛质数.质数距离;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/3 15:30
 * @description https://www.acwing.com/problem/content/198/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1000010, cnt = 0;
    static int[] primes = new int[N];
    static boolean[] st = new boolean[N];
    static int l, r;

    static void ola(int n) {
        Arrays.fill(st, false);
        cnt = 0;
        st[0] = st[1] = false;
        for (int i = 2; i <= n; i++) {
            if (!st[i]) primes[++cnt] = i;
            for (int j = 1; i * primes[j] <= n; j++) {
                st[i * primes[j]] = true;
                if (i % primes[j] == 0) break;
            }
        }
    }

    public static void main(String[] args) {
        while (sc.hasNext()) {
            ola(50000);
            Arrays.fill(st, false);
            l = sc.nextInt();
            if (l == -1) break;
            r = sc.nextInt();
            for (int i = 1; i <= cnt; i++) {
                int p = primes[i];
                for (long j = Math.max(2L * p, ((long) l + p - 1) / p * p); j <= r; j += p) {
                    st[(int) (j - l)] = true;
                }
            }

            int cnt = 0;
            for (int i = 0; i <= r - l; i++) {
                if (!st[i] && i + l >= 2) primes[++cnt] = i + l;
            }
            if (cnt < 2) out.println("There are no adjacent primes.");
            else {
                int maxp = 1, minp = 1;
                for (int i = 1; i < cnt; i++) {
                    int d = primes[i + 1] - primes[i];
                    if (d < primes[minp + 1] - primes[minp]) minp = i;
                    if (d > primes[maxp + 1] - primes[maxp]) maxp = i;
                }
                out.printf("%d,%d are closest, %d,%d are most distant.\n",
                        primes[minp], primes[minp + 1],
                        primes[maxp], primes[maxp + 1]);
            }
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}